<!--
 * @Author: daidai
 * @Date: 2022-02-20 16:16:42
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 09:18:22
 * @FilePath: \web-pc\src\pages\big-screen\view\indexs\left-center.vue
-->
<template>
  <Echart id="leftCenter" v-if="pageflag" :options="options" isClick class="left_center_inner" ref="charts"
    height="230px" @chartClick="chartClick" />
  <Reacquire v-else @onclick="getData" style="line-height: 200px">
    重新获取
  </Reacquire>
</template>

<script>
import { currentPOST } from 'api/modules';
import { colorsA, colorsB, legend, xAxis, yAxis } from '@/config/EChartVar';
import { graphic } from 'echarts';

export default {
  data() {
    return {
      options: {},
      countUserNumData: {
        axisData: ['2021', '2022', '2023', '2024', '2025'],
        seriesData: [5402.55, 5710.11, 6125.95, 6604.78, 543.5],
        seriesDataBg: [0, 0, 0, 7000, 0],
      },
      pageflag: true,
      timer: null,
      chartType: 0,
      autoTimer: null,
      series: [],
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  beforeDestroy() {
    this.clearData();
  },
  methods: {
    autoChange() {
      this.options.series = this.chartType >= 2 ? this.series : this.series[0];
      if (this.chartType > 2) {
        this.chartType = 0;
      } else {
        this.chartType += 1;
      }
    },
    clearData() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    getData() {
      this.pageflag = true;
      this.init(this.countUserNumData);

      this.autoSwitper();
    },
    //轮询
    switper() {
      if (this.timer) {
        return;
      }
      let looper = (a) => {
        this.getData();
      };
      this.timer = setInterval(
        looper,
        2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
      );
      let myChart = this.$refs.charts.chart;
      myChart.on('mouseover', (params) => {
        this.clearData();
      });
      myChart.on('mouseout', (params) => {
        this.timer = setInterval(
          looper,
          2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
        );
      });
    },
    autoSwitper() {
      if (this.autoTimer) {
        return;
      }
      let looper = (a) => {
        this.autoChange();
      };
      this.autoTimer = setInterval(
        looper,
        3000
      );
    },
    init(currentData) {
      // let axisData = ["2021年", "2022年", "2023年", "2024年"];
      let axisData = currentData.axisData;
      let seriesData = [
        [...currentData.seriesData],
        [...currentData.seriesDataBg],
      ];

      const options = {
        grid: {
          top: '15%',
          left: '4%',
          right: '8%',
          bottom: '6%',
          containLabel: true,
        },
        legend: {
          show: true,
          // x: "center",
          // y: "25",
          top: '0%',
          right: '4%',
          icon: 'circle',
          itemWidth: 12,
          itemHeight: 12,
          // itemGap: 60,
          textStyle: legend.textStyle,
          data: ['亿元'],
        },
        xAxis: {
          type: 'category',
          data: axisData,
          // boundaryGap: false,
          boundaryGap: false,
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false, // 不显示下切线
          },
          axisLabel: xAxis.axisLabel,
          axisLine: {
            lineStyle: {
              background: 'rgba(176, 215, 255, 0.4)',
            },
          },
        },
        yAxis: [
          {
            type: 'value',
            max: 8000,
            // min: 100,//
            interval: 2000,
            axisLabel: yAxis.axisLabel,
            axisLine: {
              show: false,
              // interval: 1,
              lineStyle: {
                color: 'rgba(0,0,0,0.5)',
              },
            },
            splitLine: {
              show: true,
              // interval: 1,
              lineStyle: {
                color: ['rgba(176, 215, 255, 0.2)'],
                width: 1,
                type: 'dashed',
              },
            },
            // splitLine: {
            //   show: false,
            // },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [],
      };
      const series = [
        {
          name: '亿元',
          type: 'line',
          // 动画配置
          animation: true,
          animationDuration: 2000,
          animationEasing: 'quarticInOut',
          animationDelay: function (idx) {
            return idx * 150;
          },
          smooth: false,
          showSymbol: true,
          // symbol: 'circle',
          // symbolSize: 6,
          data: seriesData[0],
          itemStyle: {
            normal: {
              // color: "#38FFBA",
              color: 'rgba(0,219,254, .7)',
            },
          },
          // areaStyle: {
          //   color: colorsB[0],
          // },
          lineStyle: {
            normal: {
              width: 4,
            },
          },
          label: {
            show: false,
            position: 'top',
            formatter: (par) => {
              return par.value;
            },
            textStyle: { fontSize: 12, color: '#FFF' },
          },
          markPoint: {
            symbol: 'circle',
            symbolSize: [14, 14], // 容器大小
            symbolOffset: ['0%', '0%'],
            data: [
              {
                name: '最大值',
                type: 'max',
                valueIndex: 1,
              },
            ],
            label: {
              show: true,
              position: 'top',
              fontSize: 12,
              // color: '#FFF',
              color: 'rgba(0,219,254, 1)',
              formatter: (par) => {
                return par.value;
              },
            },
          },
        },
        {
          type: 'bar',
          barWidth: 50,
          showBackground: false,
          backgroundStyle: {
            color: 'rgba(21,136,209,0.2)',
          },
          itemStyle: {
            color: 'rgba(225,225,225,0.2)', //数据的间隔颜色
          },
          data: seriesData[1],
          z: 0,
          zlevel: 0,
        },
      ];
      options.series = series;
      this.series = [...series];
      this.options = options;
    },
    chartClick(params) {
      console.log('点击的数据:', params);
      if (params.componentSubType != 'bar') {
        const idx = this.countUserNumData.seriesData.findIndex(item => item === params.value)
        const name = this.countUserNumData.axisData[idx]
        // 这里处理你的点击逻辑
        this.$emit('chartClick', { ...params, name }, '年市场交易额', 'LeftLeftTop');
      }
    },
  },
};
</script>
<style lang="scss" scoped></style>
